/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.AccountingDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.AccountingTBL;

/**
 *
 * @author Patrick kendzo
 * Email: pkendzo@msn.com
 */
@Stateless
public class AccountingDAO implements AccountingDAOInt{

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
	private EntityManager	em	= null;
	
	@Override
	public void delete(String todo)
	{
		AccountingTBL account = em.find(AccountingTBL.class, todo);
		em.remove(account);
		em.flush();
		
	}

	@Override
	public void persist(AccountingTBL todo)
	{
		em.persist(todo);
		em.flush();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountingTBL> retrieveAvailableUsers()
	{
		try
		{
			return (List<AccountingTBL>) em.createQuery(
					"select a from AccountingTBL a").getResultList();
		}
		catch (Exception e)
		{
			throw new RuntimeException(
					"Could not retrieve any accounts", e);
		}
	}

	@Override
	public void update(AccountingTBL todo)
	{
		em.merge(todo);
		em.flush();
		
	}

	@Override
	public AccountingTBL getUser(String login)
	{
		try
		{
			return (AccountingTBL)em.createQuery(
					"select a from AccountingTBL a where a.Login= '"+login+"'").getSingleResult();
		}
		catch (Exception e)
		{
			throw new RuntimeException(
					"Could not retrieve any accounts", e);
		}
	}

	@Override
	public AccountingTBL findAccountByLogin(String login) {
		
		return em.find(AccountingTBL.class, login);
	}

}
